package com.alstudio.module.xmpp.protocol.xmpp.parser.iq;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQType;
import com.alstudio.module.xmpp.protocol.xmpp.parser.BaseParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.utils.log.ALLog;
import com.alstudio.utils.xml.BaseXmlParser;

public abstract class ALBaseIQParser extends BaseParser implements
		ALIQPackageCallback {

	protected ALXmppEventListener mListener;
	protected BaseIqXmlParser mParser;
	protected ALIQType mIqType = ALIQType.RESULT;
	protected final String ERROR_CODE_TAG = "error";
	protected final String TRUE = "1";
	protected final String JID_TAG = "jid";
	protected final String NICK_TAG = "nick";
	protected final String SEX_TAG = "sex";
	protected final String AVATAR_TAG = "avatar";
	protected final String LAST_ONLINE_TAG = "lastonline";
	protected final String AGE_TAG = "age";
	protected final String PROVICE_TAG = "province";
	protected final String CITY_TAG = "city";
	protected final String GLAMOUR_TAG = "glamour";
	protected final String ITEMS_TAG = "items";
	protected final String ITEM_TAG = "item";
	protected final String TYPE_TAG = "type";

	protected final String VCARD_TAG = "vcard";
	protected final String PHOTOS_TAG = "photos";
	protected final String PHOTO_TAG = "photo";
	protected final String SIG_TAG = "sig";

	protected final String ONLINE_TAG = "online";
	protected final String IS_FRIEND_TAG = "isfriend";
	protected final String IN_BLACK_LIST_TAG = "inblacklist";
	protected final String VAUTHED_TAG = "vauthed";
	protected final String FAN_SNUM_TAG = "fansnum";
	protected final String IS_FAVORITE_TAG = "isfollow";
	protected final String IS_FANS_TAG = "isfans";
	//
	protected final String STATUS_TAG = "status";
	protected final String VOICE_TAG = "voiceintro";
	protected final String ZODIAC_TAG = "zodiac";
	protected final String EMAIL_TAG = "email";
	protected final String GOLD_TAG = "gold";
	protected final String SILVER_TAG = "silver";

	protected final String BIRTHDAY_TAG = "birthday";
	protected final String BACKGROUND_TAG = "background";

	protected final String VIP_LEVEL_TAG = "viplevel";
	protected final String VALUE_TAG = "value";
	protected final String IS_SPECIAL_TAG = "isspecial";
	protected final String STAR_TAG = "star";
	protected final String POINTS_TAG = "points";
	protected final String COUNT_TAG = "count";
	protected final String VERSION_TAG = "version";
	// 呼叫价格
	protected final String CALL_PRICE_TAG = "price";

	protected int code = 0;
	protected String ns;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
	}

	protected void startParse(ALIQ packact, String stream,
			IQXmlParseEventCallback listener) {
		try {
			mParser = new BaseIqXmlParser(stream, packact.getType(), listener);
			mParser.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ALLog.e(e.toString());
		} finally {
			packact = null;
			stream = null;
		}
	}

	/**
	 * 读取节点内容
	 * 
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	protected String readText() {
		try {
			return mParser.readText();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取节点所有属性
	 * 
	 * @param tag
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	protected ArrayList<String> getAttributes(final String tag)
			throws XmlPullParserException, IOException {
		return mParser.getAttributes(tag);
	}

	/**
	 * 获取属性内容
	 * 
	 * @param att
	 * @return
	 */
	protected String getAttValue(String att) {
		return mParser.getAttValue(att);
	}

	protected class BaseIqXmlParser extends BaseXmlParser {

		private IQXmlParseEventCallback mCallback;
		protected ALIQType mType;

		protected BaseIqXmlParser(String xml, ALIQType type,
				IQXmlParseEventCallback callback) throws XmlPullParserException {
			// TODO Auto-generated constructor stub
			mType = type;
			mCallback = callback;
			createParser(xml);
		}

		protected void setCallback(IQXmlParseEventCallback callback) {
			mCallback = callback;
		}

		protected void start() throws XmlPullParserException, IOException {
			IQParseEventHandler handler = new IQParseEventHandler(this, mType,
					mCallback);
			setXmlParseCallback(handler);
			handler.startParse();
		}
	}

	public abstract void onCallback();

}
